home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 1 / LIB.C < prev    next >
C/C++ Source or Header  |  1995-02-23  |  3KB  |  92 lines

  1. // File from page 92 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. /*: LIB.C -- Implementation of
  34.     example C library */
  35. /* Declare structure and functions: */
  36. #include "..\1\lib.h"
  37. /* Error testing macros: */
  38. #include <assert.h>
  39. /* Dynamic memory allocation functions: */
  40. #include <stdlib.h>
  41. #include <string.h> /* memcpy() */
  42. #include <stdio.h>
  43.  
  44. void initialize(Stash* S, int Size) {
  45.   S->size = Size;
  46.   S->quantity = 0;
  47.   S->storage = 0;
  48.   S->next = 0;
  49. }
  50.  
  51. void cleanup(Stash* S) {
  52.   if(S->storage) {
  53.      puts("freeing storage");
  54.      free(S->storage);
  55.   }
  56. }
  57.  
  58. int add(Stash* S, void* element) {
  59.   /* enough space left? */
  60.   if(S->next >= S->quantity)
  61.     inflate(S, 100);
  62.   /* Copy element into storage,
  63.   starting at next empty space: */
  64.   memcpy(&(S->storage[S->next * S->size]),
  65.       element, S->size);
  66.   S->next++;
  67.   return(S->next - 1); /* Index number */
  68. }
  69.  
  70. void* fetch(Stash* S, int index) {
  71.   if(index >= S->next || index < 0)
  72.     return 0;  /* Not out of bounds? */
  73.   /* Produce pointer to desired element: */
  74.   return &(S->storage[index * S->size]);
  75. }
  76.  
  77. int count(Stash* S) {
  78.   /* Number of elements in stash */
  79.   return S->next;
  80. }
  81.  
  82. void inflate(Stash* S, int increase) {
  83.   void* v =
  84.     realloc(S->storage,
  85.         (S->quantity + increase)
  86.         * S->size);
  87.   /* Was it successful? */
  88.   assert(v);
  89.   S->storage = v;
  90.   S->quantity += increase;
  91. }
  92.